//
//  HMWelcomeViewController.swift
//  Black22
//
//  Created by Apple on 16/11/22.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import SDWebImage

//  欢迎页面
class HMWelcomeViewController: UIViewController {

    // MARK:    --懒加载
    fileprivate lazy var userIconImageView: UIImageView = {
        let imageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //  加载远程图片
        if let url = HMUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large {
            imageView.sd_setImage(with: URL(string: url))
        }
        
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        return imageView
    }()
    fileprivate lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //  欢迎信息
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14)
        label.textAlignment = .center
        
        if let name = HMUserAccountViewModel.sharedUserAccount.userAccount?.name {
            label.text = "欢迎回来," + name
        } else {
            label.text = "欢迎回来"
        }
        
        return label
    }()
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(userIconImageView)
        view.addSubview(messageLabel)
        
        userIconImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(userIconImageView)
            make.top.equalTo(userIconImageView.snp_bottom).offset(10)
        }
    }
    
    //  页面已经展示出来的时候开启弹簧动画
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        
        startAnimation()
    }
    
    private func startAnimation() {
        userIconImageView.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        messageLabel.alpha = 0
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            self.view.layoutIfNeeded()
        
        }, completion: { (complete) -> Void in
        
            UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1
            }, completion: { (_) in
                //  切换跟视图控制器
                NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotificationName), object: nil)
            })
        
        })
    }
    
    
    

}
